package com.chart.web.controller.chart;
import com.chart.chart.domian.ChartTheme;
import com.chart.chart.service.IChartThemeService;
import com.chart.common.core.domain.AjaxResult;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;

/**
 * 主题统计
 *
 * @author 罗继全
 * 2024/7/30 15:47
 */

@RestController
@RequestMapping("/chart/theme")
public class ChartThemeController {

    @Autowired
    private IChartThemeService chartThemeService;
    /**
     * 颜色保存
     */
    @PostMapping("/colorSave")
    public AjaxResult colorSave(String themeName) {
        chartThemeService.colorSave(themeName);
        return AjaxResult.success();
    }

    /**
     * 颜色导出
     */
    @PostMapping("/colorExport")
    public AjaxResult colorExport(String themeName) {
        chartThemeService.colorExport(themeName);
        return AjaxResult.success();
    }

    /**
     * 颜色导入导出的pv统计
     */
    @GetMapping("/querySaveExportPv")
    public AjaxResult querySaveExportPv() {
        List<ChartTheme> chartThemes = chartThemeService.querySaveExportPv();
        return AjaxResult.success(chartThemes);
    }

    @GetMapping("/exportChartThemes")
    public void exportChartThemes(HttpServletResponse response) throws IOException {
        // 设置响应头
        String fileName = "ChartThemes.xlsx";
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment; filename=" +
                URLEncoder.encode(fileName, StandardCharsets.UTF_8.toString()));

        // 获取图表主题数据
        List<ChartTheme> chartThemes = chartThemeService.querySaveExportPv();

        try (Workbook workbook = new XSSFWorkbook();
             OutputStream outputStream = response.getOutputStream()) {

            // 创建工作表
            Sheet sheet = workbook.createSheet("图表主题信息");

            // 创建标题行
            String[] columns = {
                     "主题名称",   "保存次数", "导出次数"
            };
            Row headerRow = sheet.createRow(0);
            for (int i = 0; i < columns.length; i++) {
                headerRow.createCell(i).setCellValue(columns[i]);
            }

            // 填充数据
            int rowNum = 1;
            for (ChartTheme theme : chartThemes) {
                Row excelRow = sheet.createRow(rowNum++);
                excelRow.createCell(0).setCellValue(theme.getThemeName() != null ? theme.getThemeName() : "");
                excelRow.createCell(1).setCellValue(theme.getSave() != null ? theme.getSave() : 0);
                excelRow.createCell(2).setCellValue(theme.getExport() != null ? theme.getExport() : 0);
            }

            // 写入Excel文件
            workbook.write(outputStream);
            outputStream.flush(); // 确保所有数据写入输出流
        } catch (IOException e) {
            e.printStackTrace(); // 处理异常
            response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); // 设置错误状态码
        }
    }











}
